package com.malamute.core.auto.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.malamute.core.auto.bean.InterfaceWrapper;
import com.opensymphony.xwork2.ActionSupport;

public class AutoSupport {
	protected Logger log = LoggerFactory.getLogger(ActionSupport.class);
	private String projectSourcePath = ".\\src";
	private File baseDir = new File(projectSourcePath);
	private static List<String> excludeList;
	private static List<String> includeList;
	private List<File> modelFiles = new ArrayList<File>();

	static {
		excludeList = new ArrayList();
		excludeList.add(".svn");
		excludeList.add("core");
		excludeList.add("template");

		includeList = new ArrayList();
		includeList.add("model");
		includeList.add("entity");

	}

	public void setExcludeList(List<String> excludeList) {
		this.excludeList = excludeList;
	}

	public List scanModels(File baseDir) {
		// System.out.println(baseDir.getAbsolutePath());
		File[] files = baseDir.listFiles();
		for (File tmp : files) {
			if (!tmp.isFile() && !excludeList.contains(tmp.getName())) {
				scanModels(tmp);
			}
			if (tmp.isFile() && checkName(tmp.getAbsolutePath())) {
				log.info("\t" + tmp.getAbsolutePath());
				modelFiles.add(tmp);
			}
		}
		return modelFiles;
	}

	public List getModelFileList() {
		return this.scanModels(baseDir);
	}

	public static boolean checkName(String fileName) {
		boolean isShow = false;
		for (String str : includeList) {
			if (fileName.indexOf(str) != -1) {
				isShow = true;
			}
		}
		return isShow;
	}

	public static void main(String[] args) throws IOException {
		AutoSupport as = new AutoSupport();
		List<File> files = as.getModelFileList();
		new InterfaceWrapper(files.iterator().next()).createFile();

		// ArrayList test=new ArrayList();
		// test.add("a");
		// test.add("b");
		// System.out.println( test.contains("a"));
	}
}
